package com.erp.controller;


import com.erp.entity.Perms;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.service.PermsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/perms")
public class PermsController {

    @Autowired
    private PermsService permsService;

    @GetMapping("/findPermsByName")
    public Result findPermsByName(String name) {
        return new Result(ResultStatus.SUCCESS, permsService.findPermsByName(name));
    }

    @GetMapping("/getAllPerms")
    public Result getAllPerms() {
        return new Result(ResultStatus.SUCCESS, permsService.getAll());    
    }
    
    @GetMapping("/getPermsByPage")
    public Result getPermsByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "8") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<Perms> list = permsService.getAll();
        PageInfo<Perms> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }


    @PostMapping("/add")
    public Result insert(@RequestBody Perms perms) {

        if (permsService.add(perms) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @GetMapping("/delete")
    public Result delete(@PathParam("id") Integer id) {
        if (permsService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Perms perms) {

        if (permsService.update(perms) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

